<?php
/*登录模块
 *author 王建
 *time 2013-04-17
 *
 */

if (! defined('BASEPATH')) {
	exit('Access Denied');
}

class Login extends CI_Controller {
	private $table_ ; //表的前缀
	private $ip ; //ip地址
    function Login(){
        parent::__construct();
        $this->load->library('admin_common');//加载admin的公用方法
        
        $this->table_ = table_pre('real_data');
        
/*        $this->load->model('admin/M_login');
		$this->load->library('session');//加载session
		$this->load->library('admin_common');//加载admin的公用方法
		$this->load->model('admin/team/M_duty_perm');
		$this->load->model('admin/members/M_members');
		$this->load->model('admin/team/M_team');
		$this->table_ = $this->admin_common->table_pre('real_data');*/
        $this->ip = get_client_ip() ;
    }
    function show_login(){
   			@ob_clean() ;
			@session_start();
			if(isset($_SESSION['username'])){//如果登录
				@header("Location:".site_url('admin/index/frame'));
			}
    		$this->load->view('views_showlogin');
    }
    //function 处理登录页面
    function do_login(){
    	$this->load->model('admin/M_login');
    	$this->load->model('admin/team/M_duty_perm');
        $this->load->model('admin/team/M_team');
    	$username = strip_tags(html_escape(daddslashes($this->input->get_post("username",true)))) ;  //用户名
    	$passwd = strip_tags(html_escape(daddslashes($this->input->get_post("password",true)))) ;  //密码  	
    	if(empty($username)){  		
    		echo result_to_towf_new('', 0, "用户名不可以为空", null);
    		exit();
    	}elseif(empty($passwd)){
    		echo result_to_towf_new('', 0, "密码不可以为空", null);
    		exit();
    	}
    	
    	
    	$passwd =md5($passwd);
	    $sql ="SELECT `gid`,`super_admin`,`sys_user` as `username` FROM {$this->table_}common_system_user where sys_user='{$username}' and passwd='{$passwd}'  AND status = 'Y'";
	    $result = $this->M_login->query_one($sql);
	   
    	if(empty($result)){
    		write_action_log($sql,'log_manager_user_login',$username,$this->ip,0,'用户名或者密码错误,或者你没有后台的权限进行登录，请仔细的检查');
    		echo result_to_towf_new('', 0, "用户名或者密码错误,或者你没有后台的权限进行登录，请仔细的检查", null);
    		exit();
    	}else{
				$group_name = '' ;
    			if(isset($result['super_admin'] ) && $result['super_admin'] == 1 ){
	    			$group_name = '超级管理员' ;
	    		}else{
	    			$duty_id = intval($result['gid']);
	    			$sql_group_name = "SELECT `duty_name` FROM `{$this->table_}common_duty` WHERE `duty_id` = '{$duty_id}'" ;
	    			$one_res_group = $this->M_team->query_one($sql_group_name);
		    		if(isset($one_res_group['duty_name']) && $one_res_group['duty_name'] != ""){
						$group_name = $one_res_group['duty_name'];
					}
	    		}    
	   			@ob_clean() ;
	    		@session_start() ;
	    		$_SESSION['username'] = $result['username'];
	    		$_SESSION['client_ip'] = get_client_ip() ; 
	    		$_SESSION['group_name'] = $group_name ;
	    		$_SESSION['duty_id'] = $result['gid'];	
				write_action_log("no sql ",'log_manager_user_login',"{$username}",$this->ip,1,"用户{$username}登录成功");
	    		echo result_to_towf_new('', 1, "success", null);	
	    		exit();		
    	}
	    
    	
    

       }
	
	//跳转到无权限页面进行处理
	function jump_permition_html(){
		$admin  = $this->config->item('web_admin_master');
		$email = $this->config->item('web_admin_email');
		show_error(" you don't have permition to Access this page,please Contact <font color='red'>{$admin}</font> &nbsp;Email:{$email}",403,"forbidden") ;
	}
	//注销session和cookie
	public function login_out(){
		if(login_name()){
			$_SESSION['username'] = '' ;
			unset($_SESSION['username']);
			$_SESSION['client_ip'] = '' ;
			unset($_SESSION['client_ip']);
			setcookie($this->config->item('cookie_prefix').'permition',"",time()-$this->config->item('cookie_expire'),$this->config->item('cookie_path'),$this->config->item('cookie_domain'),$this->config->item('cookie_secure')) ;
			unset($_COOKIE);
			showmessage('退出成功','admin/login/show_login',3,1) ;
		}
	}

}